package kr.hkit.powerjava.chapter15;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangePanel extends JPanel implements ActionListener {
	private JButton btnChangeColor;
	private Color color;

	/**
	 * Create the panel.
	 */
	public ChangePanel() {
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		color = new Color(0, 0, 0);// 처음에 검은색으로 나옴
		btnChangeColor = new JButton("색상 변경");
		btnChangeColor.addActionListener(this);
		add(btnChangeColor, BorderLayout.SOUTH);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(color);
		g.fillRect(10, 10, 430, 280);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnChangeColor) {
			actionPerformedBtnChangeColor(e);
		}
	}
	protected void actionPerformedBtnChangeColor(ActionEvent e) {
		color = new Color((int) (Math.random() * 255.0),
				(int) (Math.random() * 255.0), (int) (Math.random() * 255.0));
		repaint();//paintComponent를 직접 불러서 호출하는 게 아니라 repaint를 써서 자동적으로 호출되게 해야함
		//왜 그래야 하는지는 나도 모름.... 그냥 그렇게 해야된대여
	}
}
